//
//  Extension.swift
//  THSLProject
//
//  Created by ninevcat on 2017/8/28.
//  Copyright © 2017年 ninevcat. All rights reserved.
//

import UIKit

// MARK: -  Global value
struct Global {
    static let width = UIScreen.main.bounds.width
    static let height = UIScreen.main.bounds.height
    static let navHeight:CGFloat = 44 + UIApplication.shared.statusBarFrame.height
    static let tabHeight:CGFloat = 49
}

class ClassicCell: UITableViewCell {
    class var identifier: String { return String.className(self) }
}

// MARK: -  UIColor extension
extension UIColor {
    
    // 16进制颜色转rgb
    convenience init(hex: Int) {
        self.init(hex: hex, alpha:1)
    }
    convenience init(hex: Int, alpha: CGFloat) {
        var r:Int!, g:Int!, b:Int!
        r = (hex & 0xFF0000) >> 16
        g = (hex & 0xFF00) >> 8
        b = hex & 0xFF
        self.init(
            red: (CGFloat(r)/255),
            green: (CGFloat(g)/255),
            blue: (CGFloat(b)/255),
            alpha:alpha)
    }
    
}


// MARK: -  String extension
extension String {
    // 获取class的名称
    static func className(_ aClass: AnyClass) -> String {
        return NSStringFromClass(aClass).components(separatedBy: ".").last!
    }
    
    func substring(_ from: Int) -> String {
        return self.substring(from: self.characters.index(self.startIndex, offsetBy: from))
    }
    
    var length: Int {
        return self.characters.count
    }
}

// MARK: -  UITableView extension
public extension UITableView {
    // 注册cell
    func registerCellClass(_ cellClass: AnyClass) {
        let identifier = String.className(cellClass)
        self.register(cellClass, forCellReuseIdentifier: identifier)
    }
    
    // 从xib注册cell
    func registerCellNib(_ cellClass: AnyClass) {
        let identifier = String.className(cellClass)
        let nib = UINib(nibName: identifier, bundle: nil)
        self.register(nib, forCellReuseIdentifier: identifier)
    }
    
    func registerHeaderFooterViewClass(_ viewClass: AnyClass) {
        let identifier = String.className(viewClass)
        self.register(viewClass, forHeaderFooterViewReuseIdentifier: identifier)
    }
    
    func registerHeaderFooterViewNib(_ viewClass: AnyClass) {
        let identifier = String.className(viewClass)
        let nib = UINib(nibName: identifier, bundle: nil)
        self.register(nib, forHeaderFooterViewReuseIdentifier: identifier)
    }
}

// MARK: -  UIViewController extension
extension UIViewController {
    func initvc(_ autoAdjusts: Bool, backgroundColor: UIColor) {
        self.automaticallyAdjustsScrollViewInsets = autoAdjusts
        self.view.backgroundColor = backgroundColor  
    }
}

// MARK: -  UIImageView extension
extension UIImageView {
    
}

// MARK: -  UIImage extension
extension UIImage {

}

// MARK: -  UIView extension
extension UIView {
}

// MARK: -  UIApplication extension
extension UIApplication {
    
}
